package com.zyl.pagergallery_kt.adapter

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.zyl.pagergallery_kt.R
import com.zyl.pagergallery_kt.pojo.PhotoItem
import kotlinx.android.synthetic.main.cell_gallery.view.*

class GalleryAdapter : ListAdapter<PhotoItem, MyViewHolder>(DIFFCALLBACK) {

    //对比是不是同一个数据，使用RecyclerView可以使用submitList来提交数据
    object DIFFCALLBACK : DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        //对应layout的View的创建   R.layout.cell_gallery
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.cell_gallery, parent, false)
        //MyViewHolder创建
        val myViewHolder: MyViewHolder = MyViewHolder(inflate)
        //点击事件
        myViewHolder.itemView.setOnClickListener {
            Bundle().apply {
                //将整个List<PhotoItem>保存到Bundle
                putParcelableArrayList("PHOTO_LIST", ArrayList(currentList))
                //将当前图片的序号保存到Bundle
                putInt("PHOTO_POSITION", myViewHolder.adapterPosition)
                //传递Bundle到指定的Fragment中
                myViewHolder.itemView.findNavController().navigate(R.id.action_galleryFragment_to_photoFragment, this)
            }
        }

        return myViewHolder
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        //在图片上方加载一个闪烁的光条
        holder.itemView.shimmerLayoutGallery.apply {
            setShimmerColor(0x55FFFFFF)
            setShimmerAngle(30)
            startShimmerAnimation()
        }

        //将图片加载到ImageView中
        Glide.with(holder.itemView)
                .load(getItem(position).previewUrl)
                .placeholder(R.drawable.ic_baseline_photo_24)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        //失败后，图片改变，停止加载
                        return false.also {
                            holder.itemView.shimmerLayoutGallery.stopShimmerAnimation()
                            holder.itemView.imageViewGallery.setImageResource(R.drawable.ic_baseline_error_24)
                        }
                    }

                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        //成功后，停止加载
                        return false.also { holder.itemView.shimmerLayoutGallery.stopShimmerAnimation() }
                    }
                })
                .into(holder.itemView.imageViewGallery)
    }

}

//MyViewHolder 具体的cell里的控件
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)